package evolution.problem;

import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.exp;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class AckleyMinimization extends AbstractMinimizationProblem {

	public static final ProblemConstraints CONSTRAINTS = new ProblemConstraints(
			2, -15, 30);

	public AckleyMinimization() {
		super(CONSTRAINTS);
	}

	/**
	 * <a href>http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/
	 * Hedar_files/TestGO_files/Page295.htm</a>
	 */
	@Override
	public double getValue(double[] x) {
		int n = x.length;

		int a = 20;
		double b = 0.2;
		double c = 2 * PI;

		double sum1 = 0;
		double sum2 = 0;

		for (int i = 0; i < n; i++) {
			sum1 = sum1 + pow(x[i], 2);
			sum2 = sum2 + cos(c * x[i]);
		}

		return -a * exp(-b * sqrt(1d / n * sum1)) - exp(1d / n * sum2) + a
				+ exp(1);
	}

	@Override
	public String getGnuplotFunction() {
		return "-20*exp(-0.2*sqrt(0.5*(x*x+y*y)))-exp(0.5*(cos(2*pi*x)+cos(2*pi*y)))+20+exp(1)";
	}

	@Override
	public String getGnuplotFunctionName() {
		return "ackley";
	}
}
